<template>
  <div class="app-container">
    <!-- 添加或修改版本管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" label-position="left"
        :hide-required-asterisk="true" :disabled="disabled">
        <el-row :gutter="60">
          <el-col :span="12">
            <el-form-item label="版本名称" prop="versionName">
              <el-input v-model="form.versionName" placeholder="请输入版本名称" maxlength="255" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-input-number v-model="form.version" placeholder="请输入版本号" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="强制更新" prop="force">
              <el-select  class="form-item-input-style" v-model="form.force" placeholder="是否强制更新">
                <el-option label="否" value="0"></el-option>
                <el-option label="是" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="更新文件" prop="url">
              <fileUpload :fileType="['apk']" :is-url="true" :fileSize="500" v-model="form.url" :deleteShow="!disabled" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="更新提示" prop="updateInformation">
              <el-input type="textarea" v-model="form.updateInformation" placeholder="请输入更新提示" maxlength="255" show-word-limit rows="3"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getVersion, addVersion, updateVersion } from "@/api/jiaopei/version";

export default {
  name: "Dialog",
  data() {
    return {
      // 是否禁用表单
      disabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        versionName: [
          { required: true, message: "版本名称不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ],
        force: [
          { required: true, message: "是否强制更新不能为空", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    /** 打开版本管理弹框 */
    async show(id, disabled) {
      this.reset();
      this.disabled = disabled
      if (this.disabled) {
        this.title = "详情"
      } else if (id) {
        await getVersion(id).then(response => {
          this.form = response.data;
        });
        this.title = "修改"
      } else {
        this.title = "新增"
      }
      this.open = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        versionName: null,
        version: null,
        force: "0",
        url: null,
        updateInformation: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVersion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh');
            });
          } else {
            addVersion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh');
            });
          }
        }
      });
    },
  }
};
</script>
